<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>功能包列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
    <style>

    </style>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">

        <div lay-filter="listSearch" class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">

                <div class="layui-inline">
                    <input type="text" name="version" placeholder="请输入版本号" autocomplete="off" class="layui-input">
                </div>

                <div class="layui-inline">
                    <select name="quick_id" lay-search>
                        <option value="0">选择应用</option>
                        {foreach $apps as $v}
                        <option value="{$v['id']}">{$v['name']}</option>
                        {/foreach}
                    </select>
                </div>

                <div class="layui-inline">
                    <select name="channel_id" lay-search>
                        <option value="0">选择投放号</option>
                        {foreach $channels as $v}
                        <option value="{$v['id']}">{$v['name']}</option>
                        {/foreach}
                    </select>
                </div>

                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                    <button type="button" class="layui-btn layui-btn-primary" lay-submit lay-filter="LAY-reset">重置</button>
                </div>

            </div>
        </div>

        <div class="layui-card-body">

            <div style="margin-bottom: 10px;">
                <button class="layui-btn layui-btn-normal layuiadmin-btn-list" data-type="add">添加</button>
            </div>

            <table id="LAY-user-back-role" lay-filter="LAY-user-back-role"></table>

            <script type="text/html" id="table-useradmin-admin">
                <a class="layui-btn layui-btn-primary layui-border-blue layui-btn-xs copySpan" data-clipboard-text="http://h5.xiayizx.cn/#/pages/login/login?code={{ d.channel_code }}" data-toggle="tooltip" data-placement="top" title="点击复制" data-original-title="点击复制">复制渠道链接</a>
                <a class="layui-btn layui-btn-primary layui-border-blue layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>

            </script>
        </div>
    </div>
</div>


<script src="/static/js/clipboard.min.js"></script>
<script src="/static/layuiadmin/layui/layui.js"></script>

<script>
    var clipboard = new Clipboard('.copySpan');
    clipboard.on('success', function(e) {
        // console.log(e);
        layer.msg("复制成功！",{icon:6})
    });

    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index','table','dropdown'], function(){
        var $ = layui.$
            ,form = layui.form
            ,table = layui.table
            ,admin = layui.admin
            ,dropdown = layui.dropdown;

        //监听搜索
        form.on('submit(LAY-user-back-search)', function(data){
            var field = data.field;

            //执行重载
            table.reload('LAY-user-back-role', {
                where: field
                ,page: {
                    curr: 1  // 将页码重置为第一页
                }
            });
        });

        form.on('submit(LAY-reset)', function(data){
            form.val("listSearch", {
                "version": "",
                "channel_id": "0",
                "quick_id": "0"
            });
            form.render();
            table.reload('LAY-user-back-role', {
                where: {"version": "","channel_id": "0","quick_id": "0"}
                ,page: {
                    curr: 1  // 将页码重置为第一页
                }
            });
        });

        table.render({
            elem: '#LAY-user-back-role'
            , url: '/version/list_data' //模拟接口
            , response: {
                statusCode: 1 //规定成功的状态码，本来是：0，现在统一改为1
            }
            ,cellMinWidth: 180
            , defaultToolbar: false
            , page: true //开启分页
            , limit: 20
            , cols: [[
                {field: 'id', width: 80, title: '序号'}
                , {field: 'quick_id', title: '应用ID'}
                , {field: 'app_name', title: '应用名称'}
                , {field: 'channel_name', title: '投放号'}
                ,{field: 'update_time', title: '更新时间', align:'center',minWidth: 160}
                , {title: '操作', minWidth: 300, align: 'center', fixed: 'right', toolbar: '#table-useradmin-admin'}
            ]]
        });


        //监听行工具事件
        table.on('tool(LAY-user-back-role)', function(obj){
            var data = obj.data;
           if(obj.event === 'del')
            {
                layer.confirm('确定删除吗？', function (index) {

                    $.ajax({
                        url: "{:url('Version/delete')}?id=" + obj.data.id,
                        type: "get",
                        dataType: "json",
                        data: {},
                        async: true,
                        success: function (rs) {
                            if (rs['code'] == 1) {

                                layer.msg(rs['msg'], function () {
                                    obj.del();
                                    layer.close(index);
                                });

                            } else {
                                layer.msg(rs['msg']);
                            }
                            return false;
                        }
                    });

                });
            }
        });

        //事件
        var active = {
            add: function(){
                layer.open({
                    type: 2
                    ,title: '添加功能包版本信息'
                    ,content: '/version/add'
                    ,offset: '5px'
                    , area: ['580px', '620px']
                    , maxmin: true//开启最大化最小化按钮
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submit = layero.find('iframe').contents().find("#LAY-data-add-submit");

                        //监听提交
                        iframeWindow.layui.form.on('submit(LAY-data-add-submit)', function(data){
                            var field = data.field; //获取提交的字段
                            //提交 Ajax 成功后，静态更新表格中的数据
                            //$.ajax({});
                            admin.req({
                                url: '/version/add'
                                , type: "POST"
                                , data: field
                                , done: function (res) {
                                    layer.msg('添加成功', {icon: 1, time: 1000}, function () {
                                        table.reload('LAY-user-back-role');
                                        layer.close(index); //关闭弹层
                                    });
                                },not_finish:function (res) {
                                    layer.msg(res.msg);
                                }
                            });
                        });

                        submit.trigger('click');
                    }
                });
            }
        }
        $('.layuiadmin-btn-list').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>

</body>
</html>